package com.test.set;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

public class MHashSet<T> {
	private  transient HashMap<T, Object> map;
	private static final Object PRESENT = new Object();
	public MHashSet(){
		map = new HashMap<T, Object>();
	}
	public MHashSet(int initialCapacity){
		map = new HashMap<T, Object>(initialCapacity);
	}
	
	public MHashSet(Collection<T> collection){
		map = new HashMap<T, Object>(Math.max((int)(collection.size() / 0.75 + 1), 16));
		addAll(collection);
	}
	
	public boolean add(T t){
		return map.put(t, PRESENT) == null;
	}
	
	public boolean addAll(Collection<T> col){
		boolean modify = false;
		for (T t : col) {
			if(add(t)){ 
				modify = true;
			}
		}
		return modify;
	}
	
	public boolean remove(T t){
		return map.remove(t) == PRESENT;
		
	}
	
	public Iterator<T> iterator(){
		return map.keySet().iterator();
	}
	public static void main(String[] args) {
		MHashSet mset = new MHashSet<>();
		mset.add(4);
		mset.add(2);
		Iterator iter = mset.iterator();
		while(iter.hasNext()){
			System.out.println(iter.next());
		}
	}
}
